import { defineStore } from 'pinia'

import {
  reqCategory1,
  reqCategory2,
  reqCategory3,
  reqFindBaseCategory3,
  reqAttr,
} from '@/api/product/attr'
import { CategoryResponse, AttrResponse } from '@/api/product/attr/type'
import type { CategoryState } from './types/type'
const useCategoryStore = defineStore('CategoryStore', {
  state: (): CategoryState => {
    return {
      c1Id: '',
      c2Id: '',
      c3Id: '',
      c1Arr: [], //菜单折叠参数
      c2Arr: [],
      c3Arr: [],
    }
  },
  actions: {
    async getCategory1() {
      let result: CategoryResponse = await reqCategory1()
      if (result.code === 200) {
        this.c1Arr = result.data
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    async getCategory2(c1id: number) {
      let result: any = await reqCategory2(c1id)
      if (result.code === 200) {
        this.c2Arr = result.data
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    async getCategory3(c2id: number) {
      let result: any = await reqCategory3(c2id)
      if (result.code === 200) {
        this.c3Arr = result.data
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    async findBaseCategory3(c3id: number[]) {
      let result: any = await reqFindBaseCategory3(c3id)
      if (result.code === 200) {
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
    // async getAttr() {
    //   let result: AttrResponse = await reqAttr(this.c1Id, this.c2Id, this.c3Id)
    //   if (result.code === 200) {
    //     return result.data
    //   } else {
    //     return Promise.reject(new Error(result.message))
    //   }
    // },
  },
  getters: {},
})

export default useCategoryStore
